/*
 * Settingshandler.java
 */

/* Copyright information:
 * 
 *  Copyright 2010 Failboat Productions. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY FAILBOAT PRODUCTIONS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAILBOAT PRODUCTIONS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Failboat Productions.
 */

package GUI;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class for handling propsfiles
 * @author Marius
 *
 */
class Settingshandler{

	private String icontype;
	private String backgroundtype;
	private String PATH = "bin/settings/";
	private String hudcolor;
	private String selectcolor;
	private int gamemode;
	private int chessboardSizeX;
	private boolean countdown;
	private boolean quick;
	private boolean clock;
	private Properties appProps;
	
	Settingshandler(){
	}

	/**
	 * Loads settings from appProperties
	 */
	void loadSettings(){
        appProps = new Properties();
        FileInputStream in = null;
		try {
			in = new FileInputStream(PATH+"appProperties");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        try {
			appProps.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
        try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		icontype = appProps.getProperty("icontype");
		backgroundtype = appProps.getProperty("backgroundtype");
		selectcolor = appProps.getProperty("selectcolor");
		countdown = "true".equals(appProps.getProperty("countdown"));
		quick = "true".equals(appProps.getProperty("quick"));
		clock = "true".equals(appProps.getProperty("clock"));
		
		try{
		chessboardSizeX = Integer.parseInt(appProps.getProperty("chessboardSizeX"));
		gamemode = Integer.parseInt(appProps.getProperty("gamemode"));
		}catch(Exception e){
			e.printStackTrace();
		}
		hudcolor = appProps.getProperty("hudcolor");
	}
	
	/**
	 * Saves settings to file appProperties
	 */
	 void saveSettings(){
		
	       appProps = new Properties();
	       FileOutputStream out = null;
			
			appProps.setProperty("icontype", icontype);
			appProps.setProperty("backgroundtype",backgroundtype);
			appProps.setProperty("chessboardSizeX",""+chessboardSizeX);
			appProps.setProperty("hudcolor",hudcolor);
			appProps.setProperty("selectcolor", selectcolor);
			appProps.setProperty("gamemode", ""+gamemode);
			appProps.setProperty("countdown",String.valueOf(countdown));
			appProps.setProperty("quick",String.valueOf(quick));
			appProps.setProperty("clock",String.valueOf(clock));
			try {
				out = new FileOutputStream(PATH+"appProperties");
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
			try{
				appProps.store(out, "Saved by Settingshandler");
				out.close();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	/**
	 * Method to load default settings
	 */
	 void loadDefault(){
        Properties defaultProps = new Properties();
        FileInputStream in = null;
		try {
			in = new FileInputStream(PATH+"defaultProperties");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        try {
			defaultProps.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
        try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		
		loadSettings();
		appProps = defaultProps;
		
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(PATH+"appProperties");
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try{
			appProps.store(out, "Saved by Settingshandler");
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		loadSettings();
		
	}


	/**
	 * @return the icontype
	 */
	 String getIcontype() {
		return icontype;
	}


	/**
	 * @param icontype the icontype to set
	 */
	 void setIcontype(String icontype) {
		this.icontype = icontype;
	}


	/**
	 * @return the backgroundtype
	 */
	 String getBackgroundtype() {
		return backgroundtype;
	}


	/**
	 * @param backgroundtype the backgroundtype to set
	 */
	 void setBackgroundtype(String backgroundtype) {
		this.backgroundtype = backgroundtype;
	}


	/**
	 * @return the chessboardSizeX
	 */
	 int getChessboardSizeX() {
		return chessboardSizeX;
	}


	/**
	 * @param chessboardSizeX the chessboardSizeX to set
	 */
	 void setChessboardSizeX(int chessboardSizeX) {
		this.chessboardSizeX = chessboardSizeX;
	}


	/**
	 * @return the hudcolor
	 */
	 String getHudcolor() {
		return hudcolor;
	}


	/**
	 * @param hudcolor the hudcolor to set
	 */
	 void setHudcolor(String hudcolor) {
		this.hudcolor = hudcolor;
	}
	
	/**
	 * @return the selectcolor
	 */
	 String getSelectcolor() {
		return selectcolor;
	}

	/**
	 * @param selectcolor the selectcolor to set
	 */
	 void setSelectcolor(String selectcolor) {
		this.selectcolor = selectcolor;
	}
	
	 public String toString(){
		return ("Icontype "+icontype+" backgroundtype "+backgroundtype+" chessboardSizeX "+chessboardSizeX+" hudcolor "+hudcolor);
		
	}

	 int getGameMode() {
		return gamemode;
	}
	
	 void setGameMode(int gamemode){
		this.gamemode = gamemode;
	}

	 boolean getCountDown() {
		return countdown;
	}
	
	 void setCountDown(boolean countdown) {
		this.countdown = countdown;
	}

	 boolean getQuick() {
		return quick;
	}
	
	 void setQuick(boolean quick) {
		this.quick = quick;
	}


	 boolean getClock() {
		return clock;
	}
	
	 void setClock(boolean clock) {
		this.clock = clock;
	}
		
}
